<?php
namespace app\components;


//邮轮楼层
// json

class Ceils extends \yii\base\Widget
{
	//获得楼层
	public function getCeils($ceils)
	{	
		if(!empty($ceils)){
			
			$result = json_decode($ceils,true);
			$num = count($result);
			if($num<=2){
				return  implode(',',$result);
			}

		   	$res = $this->cutArr($result);
		   	$res = $this->comArr($res);

			return $res;

		}else{
			return false;
		}


	}
	//分割数组提取连续数
	protected function cutArr($arr)
	{
		if(!is_array($arr)){
			return false;
		}
		sort($arr);

        $result = [];
        foreach($arr as $value) {
             if (count($result)){
		        $last_list = $result[count($result) - 1];
		        $last_value = $last_list[count($last_list) - 1];
		        if ($last_value == $value - 1){
		            array_push($result[count($result) - 1], $value);
		        }else{
		            array_push($result, array($value));
		        }
		    }else{
		        array_push($result, array($value));
		    }

        }

		return $result;

	}
	//组装数组
	protected function comArr($arr)
	{
		if(!is_array($arr)){
			return false;
		}
		$str = '';
		foreach($arr as $value){
			if(count($value) == 1){
				$str .= $value[0].',';
			}elseif(count($value) == 2){	
				$str .= implode(',',$value).',';
			}elseif(count($value) >= 3){
				$min = min($value);
				$max = max($value);
				$str .= $min.'-'.$max.',';
			}
		}
		$str = rtrim($str,',');
		return $str;

	}


}
